import EventManager from "./EventManager";

const { ccclass, property } = cc._decorator;

/**
 * 通用分数条UI
 */
@ccclass
export default class ScoreBar extends cc.Component {

    static UpdateEventName = "ScoreBarUpdate";
    /**
     * 自动指向当前显示的Stage所属的ScoreBar
     */
    static instance: ScoreBar = null;

    @property(cc.Label)
    label: cc.Label = null;

    onDestroy() {
        EventManager.removeEvent(ScoreBar.UpdateEventName, this.onCoinsUpdate, this);
    }

    onLoad() {
        EventManager.addEvent(ScoreBar.UpdateEventName, this.onCoinsUpdate, this);
    }

    onEnable() {
        ScoreBar.instance = this;
    }

    //当玩家金币有变化时
    onCoinsUpdate(coins: Number) {
        console.log('刷新UI上的金币');
        this.label.string = coins + '';
    }
}
